/*
 * @Author: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @Date: 2022-10-21 14:40:08
 * @LastEditors: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @LastEditTime: 2022-10-25 18:53:50
 * @FilePath: \v-shop\code\src\http\request.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from 'axios';
// import env from "./env"
import { Message, Loading } from 'element-ui';
import router from '../router'
let loading;
function startLoading() {
    loading = Loading.service({
        lock: true,
        text: '拼命加载中...',
        background: 'rgba(0,0,0,0.7)'
    });
}
function endLoading() {
    loading.close();
}
//创建axios实例
const service = axios.create({
    //拿到线上接口地址
    baseURL:'http://heima.9yuecloud.com:9988/api/private/v1/',
    settimeout: 50000,//超时时间
})
//请求拦截
service.interceptors.request.use(config => {
    // 在发送请求之前做些什么 验证token之类的
    // console.log("请求的数据:", config);
    if (localStorage.token) {
        config.headers.Authorization = localStorage.token;
    }
    startLoading();
    return config;//记得一定要 返回config
}, error => {
    // 对请求错误做些什么
    // console.log("请求的数据:", error);
    loading.close();
    return Promise.reject(error)
})
//响应拦截
service.interceptors.response.use(response => {
    endLoading();
    // console.log(response)
    return response
}, error => {
    const { status } = error.response;
    if (status == 401) {
        Message.error('token过期啦，请重新登录！')
        localStorage.removeItem('token')
        router.push('/login')
    }
    endLoading();
    // console.log(error)
    Message.error(error.response.data.msg)
    return Promise.reject(error)
})


export default service;